// Mouse Rotate Animator
// (c) jimon game studio

#ifndef JEH_JEMOUSEROTATEANIMATOR
#define JEH_JEMOUSEROTATEANIMATOR

#include "ISceneManager.h"
#include "IAnimator.h"
#include "jeInputController.h"

namespace je
{
	namespace scene
	{
		//! Mouse Rotate Animator
		class jeMouseRotateAnimator:public je::scene::IAnimator
		{
		private:
			device::IWindow * Window;
			input::jeInputController * InputController;
			input::jeInputController * InputControlButton;

			jeVector2di Center;
			jeVector2di WindowSize;
			
			f32 Speed;

			u1 EnableControlButton;
			u1 EnableRotateUpdate;
		public:
			//! Constructor
			jeMouseRotateAnimator(ISceneManager * SceneManager)
				:IAnimator("MouseRotateAnimator"),InputControlButton(NULL),Speed(0.25f),EnableRotateUpdate(true)
			{
				Window = SceneManager->GetRender()->GetContext()->GetWindow();
				JENEW(InputController,input::jeInputController(Window->GetMouseDriver()))
				SetControlButton(true,input::KC_MOUSE_RIGHT);
				SetMouseControllerOnCenter();
			}

			//! Desturctor
			~jeMouseRotateAnimator()
			{
				SetControlButton(false);
				JEDELETE(InputController)
			}

			//! Update Controller
			void OnUpdate()
			{
				if(WindowSize != Window->GetSize())
				{
					WindowSize = Window->GetSize();
					Center.X = WindowSize.X / 2;
					Center.Y = WindowSize.Y / 2;
					InputController->SetPosition(jeVector3df((f32)Center.X,(f32)Center.Y,0));
					return;
				}

				if(EnableControlButton && InputControlButton->IsHit())
				{
					EnableRotateUpdate=!EnableRotateUpdate;
					if(EnableRotateUpdate)
						InputController->SetPosition(jeVector3df((f32)Center.X,(f32)Center.Y,0));
				}

				if(EnableRotateUpdate)
				{
					jeVector3df CursorDrvPos = InputController->GetPosition();
					jeVector2di CursorPos((s32)CursorDrvPos.X,(s32)CursorDrvPos.Y);
					if(CursorPos != Center)
					{
						jeVector2di ChangeSpeed = CursorPos - Center;
						jeVector3df ChangePos(-ChangeSpeed.Y * Speed,-ChangeSpeed.X * Speed,0);

						ChangePos += ParentNode->GetRotation();

						if(ChangePos.X < -90)
							ChangePos.X = -90;

						if(ChangePos.X > 90)
							ChangePos.X = 90;

						ParentNode->SetRotation(ChangePos);
					}
					InputController->SetPosition(jeVector3df((f32)Center.X,(f32)Center.Y,0));
				}

				IAnimator::OnUpdate();
			}

			//! Set Speed
			void SetSpeed(f32 NewSpeed)
			{
				Speed = NewSpeed;
			}

			//! Set Control Button
			void SetControlButton(u1 Enable,input::KeyCode Control = input::KC_NULL)
			{
				EnableControlButton = Enable;
				if(InputControlButton)
					JEDELETE(InputControlButton)

				if(Control > input::KC_NULL)
					JENEW(InputControlButton,input::jeInputController(Control,Window->GetKeyDriver()))
				else
					InputControlButton = NULL;
			}

			//! Set Current Control Mode
			void SetCurrentControlMode(u1 Enable)
			{
				EnableRotateUpdate=Enable;
				if(EnableRotateUpdate)
					InputController->SetPosition(jeVector3df((f32)Center.X,(f32)Center.Y,0));
			}

			//! Set Mouse Controller On Center 
			void SetMouseControllerOnCenter()
			{
				if(WindowSize != Window->GetSize())
				{
					WindowSize = Window->GetSize();
					Center.X = WindowSize.X / 2;
					Center.Y = WindowSize.Y / 2;
				}
				InputController->SetPosition(jeVector3df((f32)Center.X,(f32)Center.Y,0));
			}

			//! Serialize
			void Serialize(core::ISerializeStream * SerializeStream)
			{
				JE_SS_BEGIN(MouseRotateAnimator)
				JE_SS(Speed)
				JE_SS(EnableControlButton)

				input::KeyCode ControlButton;

				if(JE_SS_SAVE)
					ControlButton = InputControlButton->GetKey();

				JE_SS_ENUM(ControlButton,input::KeyCode)

				JE_SS_END(MouseRotateAnimator)

				if(JE_SS_LOAD)
					SetControlButton(EnableControlButton,ControlButton);

				IAnimator::Serialize(SerializeStream);
			}
		};
	}
}

#endif
